<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<head>
    <title>Chat Example</title>
    <script type="text/javascript">
        window.onload = function() {
            var conn;
            var msg = document.getElementById("msg");
            var log = document.getElementById("log");

            function appendLog(item) {
                var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
                log.appendChild(item);
                if (doScroll) {
                    log.scrollTop = log.scrollHeight - log.clientHeight;
                }
            }
            document.getElementById("fm-query").onsubmit = function() {
                if (!conn) {
                    return false;
                }
                if (!msg.value) {
                    return false;
                }
                value = {
                    "service": "/query",
                    "data": msg.value
                }
                conn.send(JSON.stringify(value));
                return false;
            };
            document.getElementById("fm-login").onsubmit = function() {
                if (!conn) {
                    return false;
                }
                value = {
                    "service": "/auth/login"
                }
                conn.send(JSON.stringify(value));
                return false;
            };
            document.getElementById("fm-recv").onsubmit = function() {
                if (!conn) {
                    return false;
                }
                value = {
                    "service": "/recv"
                }
                conn.send(JSON.stringify(value));
                return false;
            };
            document.getElementById("fm-close").onsubmit = function() {
                if (!conn) {
                    return false;
                }
                conn.close()
                return false;
            };
            if (window["WebSocket"]) {
                conn = new WebSocket("ws://192.168.5.71:8099/ws");
                conn.onclose = function(evt) {
                    var item = document.createElement("div");
                    item.innerHTML = "<b>Connection closed.</b>";
                    appendLog(item);
                };
                conn.onmessage = function(evt) {
                    var messages = evt.data.split('\n');
                    for (var i = 0; i < messages.length; i++) {
                        var item = document.createElement("div");
                        item.innerText = messages[i];
                        appendLog(item);
                    }
                };
            } else {
                var item = document.createElement("div");
                item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
                appendLog(item);
            }
        };
    </script>
</head>

<body>

    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <div id="log"></div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <form id="fm-query">
                    <input type="submit" value="Send" />
                    <input type="text" id="msg" size="64" />
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <form id="fm-login">
                    <input type="submit" value="登录" />
                </form>
                <form id="fm-close">
                    <input type="submit" value="关闭" />
                </form>
                <form id="fm-recv">
                    <input type="submit" value="接收" />
                </form>
            </div>
        </div>
    </div>


</body>

</html>